<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { useRouter } from 'vue-router' 
import { ElMessageBox, ElMessage } from 'element-plus' 
import { useTokenStore } from '@/stores/token.js' 
import { ssrModuleExportsKey } from 'vite/module-runner'


const router = useRouter() 
const tokenStore = useTokenStore() 
//条目被点击，调用的函数
const handleCommand = (command) =>{
    // 修改判断条件，去掉空格
    if(command === 'logout') {
        //退出登录
        ElMessageBox.confirm(
            '你确认要退出吗？',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
        .then(async() => {
            //退出登录
            //情况pinia中存储的token和个人信息
            tokenStore.removeToken()

            // 2. 跳转到登录界面
            router.push('/user/logout') // 确保路由路径正确

            ElMessage({
                type: 'success',
                message: '退出登录成功',
            })
           
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消成功',
            })
        })
    } else {
        //路由跳转
        router.push('/user/'+command)
    }
}



</script>

<template>
    <!--element-plus 中的容器-->
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <!--菜单标签-->
            <el-menu class="el-menu" active-text-color="#ffd04b" background-color="#271c83"  text-color="#fff" router>
                <el-menu-item index="/title/ruleOut">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>奖学金规则公示表</span>
                </el-menu-item>
                <el-menu-item index="/title/studentScore1">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>研一学生成绩公示表</span>
                </el-menu-item>
                <el-menu-item index="/title/studentScore2">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>研二学生成绩表公示表</span>
                </el-menu-item>
                <el-menu-item index="/title/studentScore3">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>研三学生成绩表公示表</span>
                </el-menu-item>
                <el-menu-item index="/title/student">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>学生信息查询表</span>
                </el-menu-item>
                <el-sub-menu >
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/user/avatar">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>更换头像</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>研究生奖学金统计系统</div>
                <!--下拉菜单-->
                <!-- command:条目被点击后触发-->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer></el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #322796;

        &__logo {
            height: 120px;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #6b91e2;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666666;
    }
}
</style>